package us.mooresr.webtroopmanager.client.activity;

import java.util.List;

import us.mooresr.webtroopmanager.client.ClientFactory;
import us.mooresr.webtroopmanager.client.TroopManagerService;
import us.mooresr.webtroopmanager.client.TroopManagerServiceAsync;
import us.mooresr.webtroopmanager.client.events.VehicleAddedEvent;
import us.mooresr.webtroopmanager.client.events.VehicleAddedHandler;
import us.mooresr.webtroopmanager.client.events.VehicleDeletedEvent;
import us.mooresr.webtroopmanager.client.events.VehicleDeletedHandler;
import us.mooresr.webtroopmanager.client.place.VehicleListPlace;
import us.mooresr.webtroopmanager.client.ui.VehicleListView;
import us.mooresr.webtroopmanager.shared.VehicleSummary;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AcceptsOneWidget;

public class VehicleListActivity extends MyActivity implements VehicleListView.Presenter {

	
	public VehicleListActivity(VehicleListPlace place, ClientFactory clientFactory) {
		super(place, clientFactory);
	}

	@Override
	public void start(AcceptsOneWidget panel, EventBus eventBus) {
		super.start(panel, eventBus);
		
		VehicleListView vehicleListView = clientFactory.getVehicleListView();
		vehicleListView.setPresenter(this);
		panel.setWidget(vehicleListView.asWidget());
		
		populateList();
		
		
		eventBus.addHandler(VehicleAddedEvent.TYPE, new VehicleAddedHandler() {

			@Override
			public void onAddEvent(VehicleAddedEvent event) {
				VehicleListView vehicleListView = clientFactory.getVehicleListView();
				vehicleListView.addVehicle(event.getVehicleSummary());
				
			}
			
		});
		
		eventBus.addHandler(VehicleDeletedEvent.TYPE,new VehicleDeletedHandler() {
			
			@Override
			public void onDeleteVehicle(VehicleDeletedEvent event) {
				VehicleListView vehicleListView = clientFactory.getVehicleListView();
				vehicleListView.removeVehicle(event.getDeletedId());
			}
		});
		

	}


	@Override
	public void deleteVehicle(VehicleSummary vehicle) {
		Long key = vehicle.getId();
		
		if (Window.confirm("Delete " + vehicle.toString() + "?")) {
			TroopManagerServiceAsync troopManagerService = (TroopManagerServiceAsync) GWT.create(TroopManagerService.class);
			
			
			AsyncCallback<Long> callback = new AsyncCallback<Long>() {

				@Override
				public void onFailure(Throwable caught) {
					// TODO Auto-generated method stub
					
				}

				@Override
				public void onSuccess(Long result) {
					
					clientFactory.getEventBus().fireEvent(new VehicleDeletedEvent(result));
				}
			};
			
			troopManagerService.deleteVehicle(key, callback);
		}
		
	}
	
	private void populateList() {
		TroopManagerServiceAsync troopManagerService = (TroopManagerServiceAsync) GWT.create(TroopManagerService.class);
		
		
		AsyncCallback<List<VehicleSummary>> callback = new AsyncCallback<List<VehicleSummary>>() {

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(List<VehicleSummary> result) {
				
				clientFactory.getVehicleListView().setData(result);
				clientFactory.getVehicleListView().setStatus("Ready.");
			}
		};
		clientFactory.getVehicleListView().setStatus("Retrieving data...");
		troopManagerService.getAllVehicles(callback);
	}
	

}
